package com.jock.entity;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import test.FlowLayout;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.GoodsListAdapter;
import com.itboye.mengwu.app.ui.main.GoodsDetailActivity;
import com.itboye.mengwu.data.model.GoodsListModel;
import com.itboye.mengwu.data.presenter.AddGoodsPresenter;
import com.itboye.mengwu.widget.PopupWindows;
import com.itboye.mengwu.widget.PullToRefreshLayout;
import com.itboye.mengwu.widget.PullToRefreshLayout.OnRefreshListener;
import com.itboye.mengwu.widget.PullableGridView;
import com.itboye.mengwu.widget.XListView;

public class GoodsListActvity extends BaseActivity implements Observer,
		OnClickListener {
	private ImageView back;
	com.itboye.mengwu.widget.PullToRefreshLayout PullToRefreshLayout;
	private XListView goodslistview;
	private int page = 1;
	GoodsListAdapter adapter;
	public final static int PAGE_SIZE = 10;
	private EditText searchText;
	TextView renqi, xiaoliang, xinpin, jianggequjian;
	AddGoodsPresenter addGoodsPresenter;
	TextView txt_title;
	private GoodsListModel goodsListModel = new GoodsListModel();
	private PopupWindow morePopupWindow;

	private static final int ORDERCOUNTXT = 10;

	private static final int XIAOLIANGPAIXU = 11;

	private static final int PACIEDI_ISGAO = 12;

	private static final int PACIEGAO_ISDI = 13;
	private ArrayList<GoodsListModel.GoodsList> arrayListGoods=new ArrayList<GoodsListModel.GoodsList>();

	public static String order;
	/**
	 * 根据自己输入价格区间进行筛选
	 */
	private String max_cost;
	private String min_const;

	private String jiagedi;
	private String jaigegao;
	private EditText tditextJiaGeDi;
	private EditText tditextJiaGeGao;

	// private TextView txtBiaoqian6, txtBiaoqian5, txtBiaoqian4, txtBiaoqian3,
	// txtBiaoqian2, txtBiaoqian1;
	String str[] = { "今天打折哟", "促价销售", "全场一折", "金品优惠", "特价活动", "抽奖得优惠券" };

	TextView[] childView;

	int posion = 0;
	FlowLayout addpupowindowflowlayout;

	StringBuilder stringBuilder = new StringBuilder();
	int count;
	private PopupWindows windows;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.goodslistactivity;
	}

	@Override
	public void onMyCreate() {
		adapter = new GoodsListAdapter(this, arrayListGoods);
		// 配置适配器
		goodslistview.setAdapter(adapter);
		addGoodsPresenter = new AddGoodsPresenter(this);
		addGoodsPresenter.getInfo(new GoodsListModel());// ("","", "", "");
		goodslistview = (XListView) this
				.findViewById(R.id.goodslistview);
		
		
		goodsListModel = new GoodsListModel();
		((PullToRefreshLayout) this.findViewById(R.id.refresh_view))
				.setOnRefreshListener(new MyListener());
		goodsListModel.setOrder(order);
		addGoodsPresenter.getInfo(goodsListModel);
		IntentFilter filter = new IntentFilter("finish");
		registerReceiver(receiver, filter);
		txt_title.setText("商品列表");
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Intent intent2 = new Intent("set");
			sendBroadcast(intent2);
			finish();
		}
	};

	private void initevents(

	final ArrayList<GoodsListModel.GoodsList> arrayListGoods) {

		goodslistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(GoodsListActvity.this,
						GoodsDetailActivity.class);
				intent.putExtra("pid", arrayListGoods.get(arg2).getId());
				startActivity(intent);
			}
		});

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			goodsListModel = (GoodsListModel) entity.getData();
			arrayListGoods.addAll(goodsListModel.getList());
			initevents(arrayListGoods);
			adapter.notifyDataSetChanged();
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.renqi:
			onZoneHePaiXu(v);
			renqi.setTextColor(getResources().getColor(R.color.main_color));
			xiaoliang.setTextColor(getResources().getColor(R.color.black));
			xinpin.setTextColor(getResources().getColor(R.color.black));
			jianggequjian.setTextColor(getResources().getColor(R.color.black));
			break;
		case R.id.xiaoliang:
			renqi.setTextColor(getResources().getColor(R.color.black));
			xiaoliang.setTextColor(getResources().getColor(R.color.main_color));
			xinpin.setTextColor(getResources().getColor(R.color.black));
			goodsListModel.setOrder(XIAOLIANGPAIXU + "");
			addGoodsPresenter.getInfo(goodsListModel);
			jianggequjian.setTextColor(getResources().getColor(R.color.black));
			break;
		case R.id.xinpin:
			renqi.setTextColor(getResources().getColor(R.color.black));
			xiaoliang.setTextColor(getResources().getColor(R.color.black));
			xinpin.setTextColor(getResources().getColor(R.color.main_color));
			jianggequjian.setTextColor(getResources().getColor(R.color.black));
			goodsListModel.setOrder("19");
			addGoodsPresenter.getInfo(goodsListModel);
			break;
		case R.id.jianggequjian:
			onFna();
			renqi.setTextColor(getResources().getColor(R.color.black));
			xiaoliang.setTextColor(getResources().getColor(R.color.black));
			xinpin.setTextColor(getResources().getColor(R.color.black));
			jianggequjian.setTextColor(getResources().getColor(
					R.color.main_color));
			break;

		default:
			break;
		}
	}

	public void onZoneHePaiXu(View v) {
		View view = LayoutInflater.from(this).inflate(
				R.layout.more_popup_dialog, null);
		int h = this.getWindowManager().getDefaultDisplay().getHeight();
		int w = this.getWindowManager().getDefaultDisplay().getWidth();
		morePopupWindow = new PopupWindow(view, w,
				android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
		morePopupWindow.setBackgroundDrawable(new BitmapDrawable());
		morePopupWindow.setFocusable(true);
		morePopupWindow.setOutsideTouchable(true);
		morePopupWindow.showAsDropDown(v);
		// 刷新状态
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		morePopupWindow.setBackgroundDrawable(dw);

		LinearLayout linZonghe = (LinearLayout) view
				.findViewById(R.id.linZonghe);

		LinearLayout linJiageGao = (LinearLayout) view
				.findViewById(R.id.linJiageGao);
		LinearLayout linJiageDi = (LinearLayout) view
				.findViewById(R.id.linJiageDi);
		linZonghe.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				renqi.setText("综合排序");
				goodsListModel.setOrder(ORDERCOUNTXT + "");
				goodsListModel.setPage_no("1");
				addGoodsPresenter.getInfo(goodsListModel);
				morePopupWindow.dismiss();
			}
		});

		linJiageGao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				renqi.setText("价格由高到低");
				goodsListModel.setOrder(PACIEGAO_ISDI + "");
				goodsListModel.setPage_no("1");
				addGoodsPresenter.getInfo(goodsListModel);
				morePopupWindow.dismiss();
			}
		});

		linJiageDi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				renqi.setText("价格由低到高");
				goodsListModel.setOrder(PACIEDI_ISGAO + "");
				goodsListModel.setPage_no("1");
				addGoodsPresenter.getInfo(goodsListModel);
				morePopupWindow.dismiss();
			}
		});
	}

	@SuppressWarnings("deprecation")
	public void onFna() {
		View view = View.inflate(this, R.layout.add_popup_dialog, null);
		windows = new PopupWindows(this, jianggequjian, view);
		tditextJiaGeDi = (EditText) view.findViewById(R.id.tditextJiaGeDi);
		tditextJiaGeGao = (EditText) view.findViewById(R.id.editextJiaGeGao);
		LinearLayout linOne = (LinearLayout) view.findViewById(R.id.linOne);

		Button btn_finsh = (Button) view.findViewById(R.id.btn_finsh);

		childView = new TextView[str.length];// <TextView>();

		for (int i = 0; i < str.length; i++) {
			posion = i;
			addpupowindowflowlayout = (FlowLayout) view
					.findViewById(R.id.addpupowindowflowlayout);
			View vieww = LayoutInflater.from(this).inflate(
					R.layout.item_addppuwindow, null);
			TextView textView = (TextView) vieww.findViewById(R.id.txtviewadd);
			textView.setText(str[i]);

			// Log.d("hu", arrayList.get(i).getName());
			textView.setGravity(Gravity.CENTER);
			textView.setTextColor(getResources().getColor(R.color.black));
			textView.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.biaoqianshape2));
			textView.setTag(str[i]);
			textView.setTag(R.id.tag_second, i);
			// childView.add(textView);
			android.widget.RelativeLayout.LayoutParams lP = new android.widget.RelativeLayout.LayoutParams(
					android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
					android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
			lP.setMargins(10, 10, 10, 10);
			addpupowindowflowlayout.addView(vieww, lP);
			// childView[i].setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// ByAlert.alert(childView[Integer.parseInt(v.getTag() + "")]
			// .getText().toString());
			// }
			// });
		}

		btn_finsh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				windows.dismiss();
			}
		});

		Button btnCongZhi = (Button) view.findViewById(R.id.btnCongZhi);
		Button btnQueren = (Button) view.findViewById(R.id.btnQueren);
		btnQueren.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				min_const = tditextJiaGeDi.getText().toString().trim();
				max_cost = tditextJiaGeGao.getText().toString().trim();
				if (min_const == null || min_const == "" || max_cost == null
						|| max_cost == "") {
					ByAlert.alert("请输入价格区间");
				} else {
					goodsListModel.setMax_cost(max_cost);
					goodsListModel.setMin_cost(min_const);
					goodsListModel.setPage_no("1");
					addGoodsPresenter.getInfo(goodsListModel);
				}
				windows.dismiss();
			}
		});

		btnCongZhi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onChongZhi();
			}
		});

	}

	public void onChongZhi() {
		tditextJiaGeDi.setText("");
		tditextJiaGeGao.setText("");
	}

	class MyListener implements OnRefreshListener {
		private GoodsListModel goodsListModel = new GoodsListModel();

		@Override
		public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
			// 下拉刷新操作
			new Handler() {
				@Override
				public void handleMessage(Message msg) {
					page = 1;
					goodsListModel.setPage_no(page + "");
					goodsListModel.setMax_cost(max_cost);
					goodsListModel.setMin_cost(min_const);
					goodsListModel.setOrder(PACIEDI_ISGAO + "");
					addGoodsPresenter.getInfo(goodsListModel);
					// 千万别忘了告诉控件刷新完毕了哦！
					pullToRefreshLayout
							.refreshFinish(PullToRefreshLayout.SUCCEED);
				}
			}.sendEmptyMessageDelayed(0, 1000);
		}

		@Override
		public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
			// 加载操作
			new Handler() {
				@Override
				public void handleMessage(Message msg) {
					// 千万别忘了告诉控件加载完毕了哦！
					page++;
					goodsListModel.setPage_no(page + "");
					goodsListModel.setMax_cost(max_cost);
					goodsListModel.setMin_cost(min_const);
					goodsListModel.setOrder(PACIEDI_ISGAO + "");
					addGoodsPresenter.getInfo(goodsListModel);
					pullToRefreshLayout
							.loadmoreFinish(PullToRefreshLayout.SUCCEED);
				}
			}.sendEmptyMessageDelayed(0, 1000);
		}

	}

}
